﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using log4net;

namespace StockFinder.Services.Extractors.Yahoo
{
    public class YahooSymbolExtractor : ISymbolExtractor
    {
        private static readonly ILog _Log = LogManager.GetLogger(typeof(YahooSymbolExtractor));

        const string SYMBOL_PATTERN = "<tt>([\\w\\s\\.]+)</tt>";

        #region ISymbolExtractor Members

        public event Action<string> LogUpdate;        

        public List<string> ExtractSymbolsFromUrl(string url)
        {
            OnLogUpdate(string.Format("Making page request for {0}...", url));

            List<string> symbols = new List<string>();

            //create webrequest from url
            HttpWebRequest webRequest = HttpWebRequest.Create(url) as HttpWebRequest;

            HttpWebResponse webResponse = webRequest.GetResponse() as HttpWebResponse;

            string pageSource = null;

            //create the reader to extract the page source
            using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()))
            {
                OnLogUpdate(string.Format("Reading page source..."));

                //get the page source
                pageSource = streamReader.ReadToEnd();
            }

            webResponse = null;
            webRequest = null;

            MatchCollection matches = Regex.Matches(pageSource, SYMBOL_PATTERN, RegexOptions.Multiline);

            OnLogUpdate(string.Format("Found {0} matches", matches.Count));

            foreach (Match match in matches)
            {
                if (match.Groups.Count == 2)
                {
                    string symbol = match.Groups[1].Value;

                    symbols.Add(symbol);

                    OnLogUpdate(string.Format("Added {0}", symbol));
                }
            }

            return symbols;
        }

        #endregion

        protected virtual void OnLogUpdate(string logMessage)
        {
            if (LogUpdate != null)
            {
                LogUpdate(logMessage);
            }
        }        
    }
}

